<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a><cite>资源管理</cite></a>
    <a><cite>内容管理</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <div id="table_text_resource"></div>
      <script type="text/html" id="tpl_tool">
        <a class="layui-table-link layui-font-green" lay-event="edit">
          <i class="layui-icon layui-icon-edit"></i>编辑
        </a>
      </script>
    </div>
  </div>
</div>

<script>
  layui.use(['fox_table', 'tinymce'], function () {
    let $ = layui.$
      , fox_table = layui.fox_table
      , admin = layui.admin
      , form = layui.form
      , view = layui.view
      , editor = layui.tinymce

    let resourceUrl = 'admin/text_resources'

    // 列表查询
    fox_table.render({
      id: 'table_text_resource'
      , url: resourceUrl
      , page: false
      , width: 670
      , cols: [[
        {title: 'KEY', field: 'key', width: 180, align: "left"}
        , {title: '内容说明', field: 'description', minWidth: 200}
        , {title: '操作', width: 100, align: 'left', fixed: 'right', toolbar: '#tpl_tool'}
      ]]
    });

    //监听操作栏
    fox_table.toolbar(function (obj) {
      let data = obj.data;
      if (obj.event === 'edit') {
        showForm(data)
      }
    });

    //显示弹窗
    function showForm(data) {
      let title = data.description
      let minWidth = '800px';
      let minHeight = '650px';
      admin.popup({
        title: title
        , area: [minWidth, minHeight]
        , id: 'popup_content'
        , success: function (layero, index) {
          view(this.id).render('common/resource/text_form', data).done(function () {
            editor.render({
              elem: "#content"
              , height: 500
            }, function (opt, edit) {
              edit.setContent(data.content || '');
            });

            form.on('submit(submit_content)', function (d) {
                let field = d.field;
                field.content = editor.get('#content').getContent();
                if (!field.content) {
                  layer.msg('请输入内容详情')
                  return;
                }
                admin.post(resourceUrl, field, function (res) {
                  layer.close(index);
                  fox_table.reload();
                })
              }
            )
          })
        }
      })
    }

  })
</script>

